package com.jsnu.xiaochi.service;

import com.jsnu.xiaochi.dao.CommentsMapper;
import com.jsnu.xiaochi.dao.FavoriteMapper;
import com.jsnu.xiaochi.dao.FoodMapper;
import com.jsnu.xiaochi.dao.Food_commentsMapper;
import com.jsnu.xiaochi.pojo.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.awt.*;
import java.util.List;

@Service
public class DetailServiceImpl implements DetailService {
    @Resource
    FoodMapper foodMapper;
    @Resource
    FavoriteMapper favoriteMapper;
    @Resource
    CommentsMapper commentsMapper;
    @Resource
    Food_commentsMapper food_commentsMapper;

    @Override
    public Food selectFoodById(String id) {
        return foodMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Favorite> selectFavorite(String username, String foodid) {
        FavoriteExample favoriteExample = new FavoriteExample();
        favoriteExample.createCriteria().andUsernameEqualTo(username).andFoodIdEqualTo(foodid);
        return favoriteMapper.selectByExample(favoriteExample);
    }

    @Override
    public int insertFavorite(Favorite favorite) {
        return favoriteMapper.insertSelective(favorite);
    }

    @Override
    public int deleteFavorite(String username,String foodid) {
        FavoriteExample favoriteExample=new FavoriteExample();
        favoriteExample.createCriteria().andFoodIdEqualTo(foodid).andUsernameEqualTo(username);
        return favoriteMapper.deleteByExample(favoriteExample);
    }

    @Override
    public int insertComment(Comments comment) {
        return commentsMapper.insertSelective(comment);
    }

    @Override
    public List<Comments> getAllComments(String foodId) {
        return commentsMapper.getAllComments(foodId);
    }

    @Override
    public int insertFoodComment(Food_comments food_comments) {
        return food_commentsMapper.insert(food_comments);
    }


    @Override
    public int getComentsCountByFoodid(String id) {
        Food_commentsExample food_commentsExample=new Food_commentsExample();
        food_commentsExample.createCriteria().andFoodIdEqualTo(id);
        return food_commentsMapper.selectByExample(food_commentsExample).size();
    }
}
